//
//  HomeController.m
//  GuShiNews
//
//  Created by huachun on 2019/8/16.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "HomeController.h"
#import "HomeCell.h"
#import "JiaoLModel.h"
#import "HomeChooseHeaderView.h"
#import "HomeAction.h"
#import "RootSuperAction.h"
#import "HomeLunTVModel.h"
#import "LunTanModel.h"

@interface HomeController ()<GetHomeLunDelegate>{
    NSInteger index;
    NSInteger page;
}

@property (nonatomic, strong)UITableView *mytable;

@end

@implementation HomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setnavback];
    index = 1;
    
    self.Data_array = [NSMutableArray new];
    
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:[JonesButtonItem PublicBarButtonItemPian], [JonesButtonItem NavImgButton:[UIImage imageNamed:@"fabu"] andaction:@selector(fabu) andtarget:self], nil];
    
    _mytable = [[UITableView alloc] initWithFrame:RECR style:UITableViewStylePlain];
    _mytable.dataSource          = self;
    _mytable.delegate            = self;
    _mytable.separatorInset      = UIEdgeInsetsMake(0, 15, 0, 0);
    _mytable.separatorStyle      = UITableViewCellSeparatorStyleNone;
    _mytable.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    _mytable.emptyDataSetSource   = self;
    _mytable.emptyDataSetDelegate = self;
    [self.view addSubview:_mytable];
    _mytable.backgroundColor = backColor;
    
    [_mytable registerClass:[HomeChooseHeaderView class] forHeaderFooterViewReuseIdentifier:@"header"];
    self.navigationItem.leftBarButtonItems=nil;
    MJHiCarHeader *header           = [MJHiCarHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadnew)];
    header.automaticallyChangeAlpha    = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden           = YES;
    _mytable.mj_header          = header;
    _mytable.mj_header.automaticallyChangeAlpha = YES;
    _mytable.mj_footer           = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self laodmore];
    }];
    _mytable.mj_footer.hidden    = YES;
    [_mytable.mj_header beginRefreshing];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loadnew)
                                                 name:@"FABUCHNEGGONG"
                                               object:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_mytable reloadData];
}

- (void)loadnew{
    page = 0;
    self.TableView.mj_footer.hidden    = YES;
    [self laod_net_dic];
}

- (void)laodmore{
    LunTanModel *modellin = [self.Data_array lastObject];
    page = [modellin.lun_id integerValue];
    [self laod_net_dic];
}

- (void)laod_net_dic{
    NSMutableDictionary *postdic = [NSMutableDictionary new];
    [postdic setValue:[NSString stringWithFormat:@"%li",(long)page] forKey:@"page"];
    [postdic setValue:[UserModel info].userid?[UserModel info].userid:@"0" forKey:@"user_id"];
    [postdic setValue:[NSString stringWithFormat:@"%li",(long)index] forKey:@"type"];
    HomeLunTVModel *viewmodel = [[HomeLunTVModel alloc] init:postdic];
    viewmodel.delegate = self;
}

-(void)GetHomeLunSucces:(int)value array:(NSMutableArray *)array page:(NSInteger)page{
    if (value == 1) {
        if (page==0) {
            [self.Data_array removeAllObjects];
        }
        [self.Data_array addObjectsFromArray:array];
        
        if (array.count<15) {
            _mytable.mj_footer.hidden = YES;
        }else{
            _mytable.mj_footer.hidden = NO;
        }
    }
    [_mytable.mj_header endRefreshing];
    [_mytable.mj_footer endRefreshing];
    [_mytable reloadData];
}

#pragma mark - DZNEmptyDataSetSource
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"error"];
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    NSString *text;
    NSMutableAttributedString *attStr;
    if (index==0) {
        if ([UserModel info].userid) {
            text = @"关注喜欢的人可查看";
        }else{
            text = @"请先登录";
        }
        attStr = [[NSMutableAttributedString alloc] initWithString:text];
        [attStr addAttribute:NSFontAttributeName
                       value:[UIFont systemFontOfSize:16.0]
                       range:NSMakeRange(0, text.length)];
    }else{
        text = @"无数据?点击重新加载";
        attStr = [[NSMutableAttributedString alloc] initWithString:text];
        [attStr addAttribute:NSFontAttributeName
                       value:[UIFont systemFontOfSize:14.0]
                       range:NSMakeRange(0, text.length)];
    }
    [attStr addAttribute:NSForegroundColorAttributeName
                   value:[JonesClass stringTOColor:@"#cccccc"]
                   range:NSMakeRange(0, text.length)];
    return attStr;
}

#pragma mark - DZNEmptyDataSetDelegate
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    if (index==0) {
        if (![UserModel info].userid) {
            [RootSuperAction Actiontodone:JonesGOTO_Login view:self dictionary:@{}];
            return;
        }
    }
    [self.TableView.mj_header beginRefreshing];
}

- (void)fabu{
    if ([UserModel info].userid) {
        [HomeAction Actiontodone:JonesGOTO_Home_Fabu view:self dictionary:@{}];
    }else{
        [RootSuperAction Actiontodone:JonesGOTO_Login view:self dictionary:@{}];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HomeChooseHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    view.selectblock = ^(NSInteger type) {
        self->index = type;
        [self.mytable.mj_header beginRefreshing];
    };
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 45;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.Data_array.count;
}

#pragma mark - UITableView DataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeCell *cell = [HomeCell cellForTableView:tableView cellForRowAtIndexPath:indexPath];
    cell.Model = [self.Data_array objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - UITableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LunTanModel *model = [self.Data_array objectAtIndex:indexPath.row];
    
    if (model.Img_array.count>0) {
        return 160+(RECRWITCH-40)/3 + 10;
        
    }
    
    return 160;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LunTanModel *model = [self.Data_array objectAtIndex:indexPath.row];
    [HomeAction Actiontodone:JonesGOTO_Home_Info view:self dictionary:@{@"model":model}];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
